
export function serialize(data) {
    if(!data) return '';
    let pairs = [];
    for (let name in data) {
        if(!data.hasOwnProperty(name)) continue;
        if(typeof data[name] === 'function') continue;
        let value = data[name].toString();
        name = encodeURIComponent(name);
        value = encodeURIComponent(value);
        pairs.push(name+'='+value);
    }
    return pairs.join('&');
}

export function stringFormat() {
    if (arguments.length == 0)
        return null;
    var str = arguments[0];
    for (var i = 1; i < arguments.length; i++) {
        var re = new RegExp('\\{' + (i - 1) + '\\}', 'gm');
        str = str.replace(re, arguments[i]);
    }
    return str;
}

export function checkStatus(response) {
    if (response.status >= 200 && response.status < 300) {
        return response;
    }
    const error = new Error(response.statusText);
    error.response = response;
    throw error;
}

export function parseJSON(response) {

    if (response.status === 204) {
        return undefined;
    }else {

        return response.json();
    }
}


export function tojson(response) {
    // console.log(response);
    if( response.data &&  response.data.is_success===false)
    {
        const error = new Error(response.msg);
        error.response = response;
        throw error;
    }
   return response;
}
